package  
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Minfang
	 */
	public class MatrixBox extends Sprite 
	{
		protected var data:Array;
		protected var error:int;
		
		public var real_width:int;
		public var real_height:int;
		
		public var line_mask:Sprite=new Sprite;
		public var line_setting:Object = null;

		public var background:Sprite = new Sprite;
		public var background_color:Object = null;
		
		public var row_ys:Array = new Array;
		public var column_xs:Array = new  Array;
		
		
		
		public function MatrixBox(cont:Object,textcss:String) 
		{
			error = 0;
			
			var da:Array = cont['data'];
			
			addChild(background);
			
			if (cont['background_color'] != undefined) {
				background_color = new Object;
				background_color['color'] = cont['background_color'];
			}
			
			
			var className:String = "";
			if (cont['class'] != undefined) {
				className = cont['class'];
			}
			
			if (cont['css'] != undefined) {
				textcss = cont['css'];
			}
			
			data = new Array;
			var i1:int, i2:int, i3:int;
			var t1:Array;
			var tf:TextField;
			
			var tcss:StyleSheet = null;
			
			
			
			
			
			for (i1 = 0; i1 < da.length; i1++) {
				if (typeof(da[i1]) != 'object') {
					trace(da[i1] , " is not a object");
					error++;
					return ;
				}
				t1=new Array;
				for (i2 = 0; i2 < da[i1].length; i2++) {
					if (typeof(da[i1][i2]) != 'string') {
						trace(da[i1][i2] , " is not a string");
						error++;
						return ;
					}	
					tf = new TextField;
					
					if (textcss != null) {
						
						
						tf.styleSheet = new StyleSheet;
						tf.styleSheet.parseCSS(textcss);
						
					}
					tf.htmlText ="<p class='"+className+"'>"+da[i1][i2]+"</p>";
					
					addChild(tf);
					t1.push(tf);
				}
				data.push(t1);
			}
			
			if (cont['line_setting'] != undefined) {
				line_setting = cont['line_setting'];
			}
			addChild(line_mask);
			
		}
		public function getOneElementWidth(x:int, y:int) :int{
			if (data == null) {
				return -1;
			}
			if (data[y] == undefined || data[y][x] == undefined)
			{
				return -1;
			}
			
			
			
			return  data[y][x].textWidth;
		}
		
		public function getOneElementHeight(x:int, y:int) :int{
			if (data == null) {
				return -1;
			}
			if (data[y] == undefined || data[y][x] == undefined)
			{
				return -1;
			}
			
			
			
			return  data[y][x].textHeight;
		}
		
		public function isError():int {
			return error;
		}
		
		public function getRowNumber():int {
			if (error) {
				return 0;
			}
			
			return data.length;
			
		}
		
		public function getColumnNumber():int {
			if (error) {
				return 0;
			}
			
			var col:int = 0;
			for (var i1:int = 0; i1 < data.length; i1++) {
				if (col == 0) {
					col = data[i1].length;
				}
				else if (col != data[i1].length) {
					return 0;
				}
			}
			return col;
			
		}
		
		public function findMaxColumnLength(re:Array):Array {
			
			
			for (var i1:int = 0; i1 < data.length; i1++) {
				
				
				
				for (var i2:int = 0; i2 < data[i1].length; i2++) {
					
					
					
					if ( re[i2] == undefined || re[i2] < data[i1][i2].textWidth) {
						re[i2]=data[i1][i2].textWidth;
					}
					
					
				}
			}
			return re;
			
		}
		
		public function findMaxRowHight(re:Array):Array {
			
			
			for (var i1:int = 0; i1 < data.length; i1++) {
				
				
				
				for (var i2:int = 0; i2 < data[i1].length; i2++) {
					
					
					
					if (re[i1] == undefined || re[i1] < data[i1][i2].textHeight) {
						re[i1]=data[i1][i2].textHeight;
					}
					
					
				}
			}
			return re;
			
		}
		public function setColumnsX(widthes:Array, start_index:int):void {
			
			real_width = 0;
			for (var i1:int = 0; i1 < data.length; i1++) {
				data[i1][0].x = 0;
				data[i1][0].width = widthes[start_index ];
				if (i1 == 0) {
					real_width += widthes[start_index ];
					column_xs[0] = 0;
				}
				var s:int = 0;
				
				
				for (var i2:int = 1; i2 < data[i1].length; i2++) {

					data[i1][i2].width = widthes[start_index + i2 ];
					
					s += widthes[start_index + i2 - 1];
					data[i1][i2].x = s;
					
					if (i1 == 0) {
						real_width += widthes[start_index + i2];
						column_xs[i2] = s;
					}
				}
			}
			
			
			
		}
		
		public function setRowsY(heights:Array, start_index:int):void {
			
			var s:int = 0;
			real_height = 0;
			for (var i1:int = 0; i1 < data.length; i1++) {
				
				if (i1 != 0) {
					s += heights[start_index + i1 - 1];
					row_ys[i1] = s;
				}
				else {
					row_ys[0] = 0;
				}
				real_height += heights[start_index + i1];
				
				for (var i2:int = 0; i2 < data[i1].length; i2++) {
					
					data[i1][i2].y = s;

				}
			}
		}	
		
		private function setupLineStyle(lineStyles:Object, lineName:String):void {
			
			trace("start setup");
			if (lineStyles == null) {
				return;
			}
			var lineStyle:Object;
			if (lineStyles[lineName] == undefined) {
				if (lineStyles['default'] == undefined) {
					return;
				}
				else {
					lineStyle = lineStyles['default'];
				}
			}
			else {
				lineStyle = lineStyles[lineName];
			}
			
			
			
			if (lineStyle['bmp'] != undefined && lineStyle['bmp'].length !=0 && lineStyle['bmp'][0].length !=0) {
				//bmp line
				var line_style_bmp:Array = lineStyle['bmp'];
				var bh:int = line_style_bmp.length;
				var bw:int = line_style_bmp[0].length;
				var bmp:BitmapData = new BitmapData(bw, bh, false, 0x000000);
				var row:int, col:int;
				for (row = 0; row < bh; row++) {
					for (col = 0; col < bw; col++) {
						if (line_style_bmp[row][col] != undefined) {
							bmp.setPixel(col, row, line_style_bmp[row][col]);
							
						}
						else {
							bmp.setPixel(col, row, 0xffffff);
						}
						
						
					}
				}
				line_mask.graphics.lineStyle(bh);
				line_mask.graphics.lineBitmapStyle(bmp);
			}
			else {
				//normal line;
				var lineWidth:int=1;
			
				if (lineStyle['line_width' ] != undefined) {
					lineWidth = lineStyle['line_width' ];
				}
				
				
				var line_color:int = 0x000000;
				
				
				if (lineStyle['color'] != undefined) {
					line_color = lineStyle['color'];
				}
				
				line_mask.graphics.lineStyle(lineWidth,line_color);
			}
		}
		
		public function re_draw(lineStyles:Object):void {
			
			this.graphics
			if (line_setting != null)
			{
				
			
			
		
				
				
				var i1:int;
				if (line_setting['inter_row_line'] != null) {
					
					
					
					for (i1 = 1 ; i1 < row_ys.length; i1++) {
						setupLineStyle(lineStyles, line_setting['inter_row_line']);
						line_mask.graphics.moveTo(0, row_ys[i1]);
						line_mask.graphics.lineTo(real_width, row_ys[i1]);
					}
				}
				
				if (line_setting['inter_column_line'] != null ) {
					//trace("draw inter column lines!");
					
					for (i1 = 1 ; i1 < column_xs.length; i1++) {
						setupLineStyle(lineStyles, line_setting['inter_column_line']);
						line_mask.graphics.moveTo( column_xs[i1],0);
						line_mask.graphics.lineTo(column_xs[i1],real_height);
					}
				}
				
				if (line_setting['left_line'] != null ) {
					
					setupLineStyle(lineStyles, line_setting['left_line']);
					line_mask.graphics.moveTo( 0,0);
					line_mask.graphics.lineTo(0,real_height);
					
				}
				if (line_setting['right_line'] != null ) {
				
					setupLineStyle(lineStyles, line_setting['right_line']);
					line_mask.graphics.moveTo(real_width,0);
					line_mask.graphics.lineTo(real_width,real_height);
				
				}
				
				if (line_setting['top_line'] != null ) {
				
					setupLineStyle(lineStyles, line_setting['top_line']);
					line_mask.graphics.moveTo(0,0);
					line_mask.graphics.lineTo(real_width,0);
				
				}
				
				if (line_setting['bottom_line'] != null ) {
				
					setupLineStyle(lineStyles, line_setting['bottom_line']);
					line_mask.graphics.moveTo(0,real_height);
					line_mask.graphics.lineTo(real_width, real_height);
				
				}
			
				
			
			}//end draw line
		
			if (background_color != null && background_color['color'] != undefined) {
					
				background.graphics.beginFill(background_color['color']);	
				background.graphics.drawRect(0,0,real_width, real_height);
				background.graphics.endFill();
			}
		
		} //end function
		
		public function print_element_coordinate():void {
			for (var row:int = 0;row < data.length;row++) {
				
				
				var message:String = new String;
				
				for (var col:int = 0; col < data[row].length; col++) {
					message += "(x:" + data[row][col].x + ",y:" + data[row][col].y +
					",width:" + data[row][col].width + ",height:" + data[row][col].height+") ";
				}
				//trace("    ",row, "--", message);
			}
		}
		
		
		public function printContentValue():void {
			
				for (var row:int = 0;row < data.length;row++) {
				
				
				var message:String = new String;
				
				for (var col:int = 0; col < data[row].length; col++) {
					message += "(" + data[row][col].htmlText + ") ";
				}
				
			}
		}
		
	}
	
	

}